package com.gitee.jktantan.datasource.monitor.spring.boot.autoconfig;

import com.gitee.jktantan.datasource.monitor.config.DataSourceMonitorRegister;
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import com.ryantenney.metrics.spring.config.annotation.MetricsConfigurerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 自动配置开启Metrics以及自动扫描
 */
@Configuration
@EnableMetrics
@ComponentScan(basePackages = {"com.gitee.jktantan.datasource.monitor"})
@EnableConfigurationProperties({DataSourceMonitorProperties.class})
@AutoConfigureBefore({DataSourceAutoConfiguration.class})
@Slf4j
public class DataSourceMonitorAutoConfigurer extends MetricsConfigurerAdapter {


    @Bean
    @ConditionalOnMissingBean
    public DataSourceMonitorRegister dataSourceMonitorProperties(DataSourceMonitorProperties dataSourceMonitorProperties) {
        log.debug("注册数据源监控");
        DataSourceMonitorRegister dataSourceMonitorConfig = new DataSourceMonitorRegister();
        dataSourceMonitorConfig.setEnable(dataSourceMonitorProperties.isEnable());
        return dataSourceMonitorConfig;
    }

//    @Override
//    public void configureReporters(MetricRegistry metricRegistry) {
//        registerReporter(ConsoleReporter
//                .forRegistry(metricRegistry)
//                .build())
//                .start(10, TimeUnit.SECONDS);
//    }

}
